#include "MatrixLine.h"
//////////////////////////////////////////////////////////////////////////

cMatrixLine::cMatrixLine(cInfoUnion *info,cGraphics *graphics)
{
	this->s_InfoUnion = info;
	this->m_IfShow = false;
	this->m_Color = D3DCOLOR_RGBA(255,255,255,255);
	this->m_IsStructChanged = true;
	this->m_VB = NULL;
	this->s_Device = graphics->GetDeviceCOM();
	this->m_VertexNum = 0;
}

cMatrixLine::~cMatrixLine()
{
}


LPDIRECT3DVERTEXBUFFER9	cMatrixLine::GetVB()
{
	if(this->m_IsStructChanged == false&& m_VB!=NULL)
	{
		return this->m_VB;
	}

	this->m_IsStructChanged = false;

	if(this->m_VB != NULL)
	{
		this->m_VB->Release();
		this->m_VB = NULL;
	}

	CUSTOMVERTEX *vertex;
	int LineNumRow;
	int LineNumCloumn;

	int UnitWidth = s_InfoUnion->MatrixUnitWidth;
	int UnitHeight = s_InfoUnion->MatrixUnitHeight;

	LineNumRow = s_InfoUnion->ScreamHeight/UnitHeight+1;
	LineNumCloumn = s_InfoUnion->ScreamWidth/UnitWidth+1;
	vertex = new CUSTOMVERTEX[LineNumRow*2+LineNumCloumn*2];

	this->m_VertexNum = LineNumRow*2+LineNumCloumn*2;

	int temp = 0;
	for (int count = 0;count<LineNumRow;count++)
	{
		vertex[temp].x=0.0f;
		vertex[temp].y=UnitHeight*count;
		vertex[temp].z=0.5f;
		vertex[temp].rhw=1.0f;
		vertex[temp].color=m_Color;
		temp++;
		vertex[temp].x=s_InfoUnion->ScreamWidth;
		vertex[temp].y=UnitHeight*count;
		vertex[temp].z=0.5f;
		vertex[temp].rhw=1.0f;
		vertex[temp].color=m_Color;
		temp++;
	}
	//temp=LineNumRow*2;
	for (int count = 0;count<LineNumCloumn;count++)
	{
		vertex[temp].x=UnitWidth*count;
		vertex[temp].y=0.0f;
		vertex[temp].z=0.5f;
		vertex[temp].rhw=1.0f;
		vertex[temp].color=m_Color;
		temp++;
		vertex[temp].x=UnitWidth*count;
		vertex[temp].y=s_InfoUnion->ScreamHeight;
		vertex[temp].z=0.5f;
		vertex[temp].rhw=1.0f;
		vertex[temp].color=m_Color;
		temp++;
	}

	if( FAILED( this->s_Device->CreateVertexBuffer( (LineNumRow*2+LineNumCloumn*2)*sizeof(CUSTOMVERTEX),
		0, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT, &this->m_VB, NULL ) ) )
	{
		MessageBox(NULL,(LPCSTR)"CreateVertexBuffer failed",(LPCSTR)"error",MB_OK);
		return false;
	}

	void* pVertices;
	if( FAILED( this->m_VB->Lock( 0, m_VertexNum*sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 ) ) )
		return false;
	memcpy( pVertices, vertex, m_VertexNum*sizeof(CUSTOMVERTEX) );
	this->m_VB->Unlock();

	

	return this->m_VB;
}

BOOL cMatrixLine::Render()
{
	if (this->m_IfShow == false)
	{
		return FALSE;
	}
	
	//CPaintDC pDC(this);

	//CPen pen; //A1
	//CPen *oldPen; //A2
	//pen.CreatePen(PS_SOLID, 0,RGB(255,0,0));//B
	//oldPen=pDC->SelectObject(&pen); //C
	//pDC->MoveTo(10,10); //D
	//pDC->LineTo(100,10); //E
	//pDC->LineTo(100,100); //F
	//pen.DeleteObject(); //G
	//pDC->SelectObject(oldPen); //H

	//s_Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	//s_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
	s_Device->SetStreamSource( 0, this->GetVB(), 0, sizeof(CUSTOMVERTEX) );
	s_Device->SetFVF( D3DFVF_CUSTOMVERTEX );
	s_Device->DrawPrimitive( D3DPT_LINELIST, 0, m_VertexNum/2);

	return TRUE;
}

BOOL cMatrixLine::Execute(unsigned char *keystate, BOOL *locks, int ex )
{
	return MY_MATRIX_LINE_DEFAULT;
}

BOOL cMatrixLine::ReceiveDirective(int message)
{
	return MY_MATRIX_LINE_DEFAULT;
}

BOOL cMatrixLine::TurnOn()
{
	this->m_IfShow = true;

	return TRUE;
}

BOOL cMatrixLine::TurnOff()
{
	this->m_IfShow = false;

	return TRUE;
}

void cMatrixLine::SetIfShow(bool ifshow)
{
	m_IfShow = ifshow;
}

BOOL cMatrixLine::SetColour(D3DCOLOR color)
{
	this->m_Color = color;

	return TRUE;
}